import csv
import os
from pathlib import Path


def recursive_files(dir_path, parent_path):
    dir_path = Path(dir_path)
    file_names = []
    for root, dirs, files in os.walk(dir_path):  
        root = Path(root)
        for file in files:
            abs_path = Path(root/file)
            rel_path = abs_path.as_posix().replace(Path(parent_path).as_posix(), '')
            file_names.append(rel_path[1:])
    return file_names

# 取得人的id
def get_people_id(filepath):
    filename = filepath.split('/')[-1]
    return int(filename[:3])

def write_csv(csv_file, csv_data):
    # 保存train csv
    with open(csv_file, mode='w', newline='') as file:  
        writer = csv.writer(file)  
        # 写入数据行  
        for row in csv_data:  
            writer.writerow(row)

def intra_class(file1, file2): # 判断是否出自同一个手掌
    pid1 = get_people_id(file1)
    pid2 = get_people_id(file2)
    if not pid1 == pid2:
        return False
    if ('L_' in file1 and 'L_' in file2) or ('R_' in file1 and 'R_' in file2):
        return True
    else:
        return False